package com.kfgj.hkp.manager.history.impl;

import com.kfgj.hkp.manager.AbstractManager;
import com.kfgj.hkp.common.context.CurrentContext;
import com.kfgj.hkp.common.dataaccess.PageResult;
import com.kfgj.hkp.common.dataaccess.mybatis.IMyBatisBaseDao;
import com.kfgj.hkp.common.exception.BusinessException;
import com.kfgj.hkp.common.user.entity.BaseUserEntity;
import com.kfgj.hkp.common.utils.CodeGenUtils;
import com.kfgj.hkp.common.utils.OperateFillUtils;
import com.kfgj.hkp.dao.history.IAddressBookDbDao;
import com.kfgj.hkp.dao.history.IAtsigninDbDao;
import com.kfgj.hkp.dao.history.ITextLegalDbDao;
import com.kfgj.hkp.entity.history.AddressBookEntity;
import com.kfgj.hkp.entity.history.AtsigninEntity;
import com.kfgj.hkp.entity.history.TextLegalEntity;
import com.kfgj.hkp.manager.history.ITextLegalManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description manager
 * @Auth xpp
 * @Date 2018/12/8
 * @Version 1.0
 */
@Service
public class TextLegalManagerImpl extends AbstractManager<TextLegalEntity> implements ITextLegalManager {


    @Autowired
    private ITextLegalDbDao textLegalDbDao;
    @Autowired
    private IAddressBookDbDao addressBookDbDao;
    @Autowired
    private IAtsigninDbDao atsigninDbDao;


    @Override
    protected IMyBatisBaseDao<TextLegalEntity, Long> getMyBatisDao() {
        return this.textLegalDbDao;
    }


    @Override
    public int addressbookInsert(List<AddressBookEntity> addressbookList) {
        //先删除所属人的,再新增
        BaseUserEntity userInfo = CurrentContext.getUserInfo();
        String userCode = userInfo.getUserCode();
        if(null==userCode){
            return 0;
        }



        Map<String, Object> param = new HashMap<String, Object>();
        param.put("empCode",userCode);
        int deleteSize = addressBookDbDao.batchDeleteByEmpCode(param);

        //int count = addressBookDbDao.batchInsert(addressbookList);
        for(AddressBookEntity d:addressbookList){
            List<AddressBookEntity> dw =new ArrayList<AddressBookEntity>(3);
            for(int i=0;i<d.getPhoneNumberList().size();i++){
                AddressBookEntity dwd=new AddressBookEntity();

                    dwd.setPhoneName(d.getPhoneName());
                    dwd.setPhoneNumber(d.getPhoneNumberList().get(i));
                    dwd.setEmpCode(userInfo.getUserCode());
                    dwd.setEmpName(userInfo.getUserName());
                    dwd.setCreateTime(new Date());
                    dwd.setLastUpdateTime(new Date());
                    dwd.setIsDeleted(false);
                    dwd.setCpyCode("kfwy");
                    dw.add(dwd);

            }
            int count = addressBookDbDao.batchInsert(dw);
        }
        return -1;
    }

    @Override
    public int atsigninAdd(AtsigninEntity atsigninEntity) {
        //1分钟内防止重复签到

        BaseUserEntity userInfo = CurrentContext.getUserInfo();
        String userCode = userInfo.getUserCode();
        Date nowDay = new Date();
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -1);// 1分钟之前的时间
        Date beforeD = beforeTime.getTime();
        SimpleDateFormat formatFen = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss.SSS");
        String nowDayStr = formatFen.format(beforeD);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("ownerCode",userCode);
        param.put("createTimeStart", beforeD);
        param.put("createTimeEnd", nowDay);
        List<AtsigninEntity> dbList = atsigninDbDao.selectByMap(param, "create_time", false);
        if(null!=dbList){
            if(dbList.size() >= 1){
                AtsigninEntity asidbEntity = dbList.get(0);
                Date createDbTime = asidbEntity.getCreateTime();
                long diff = nowDay.getTime() - createDbTime.getTime();//这样得到的差值是微秒级别
                long miao = diff/1000;//微秒除以1000得秒
                long m60  = 60;
                long jishi = m60 - miao;
                throw new BusinessException("1分钟内已签到!!!("+jishi+")秒后可签第二次.");
            }
        }

        //加入生成编码
        String genCode = CodeGenUtils.generate("qd");
        atsigninEntity.setQdCode(genCode);
        //加入创建人修改人
        OperateFillUtils.fill(atsigninEntity);
        List<AtsigninEntity> asiList = new ArrayList<AtsigninEntity>();
        asiList.add(atsigninEntity);
        int i = atsigninDbDao.batchInsert(asiList);
        return i;
    }

    @Override
    public PageResult<AtsigninEntity> atsigninQuery(AtsigninEntity atsigninEntity, Integer startInt, Integer pageSize, Date startTime, Date endTime) {
        BaseUserEntity userInfo = CurrentContext.getUserInfo();
        String userCode = userInfo.getUserCode();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("ownerCode",userCode);
        if(null!=startTime){
            param.put("createTimeStart",startTime);
        }
        if(null!=endTime){
            param.put("createTimeEnd",endTime);
        }
        PageResult<AtsigninEntity> pageResult = atsigninDbDao.selectByMap(param, startInt, pageSize, "create_time", false);
        return pageResult;
    }


    public Boolean npandu(String phoneName,String phoneNumber){
        BaseUserEntity userInfo = CurrentContext.getUserInfo();
        Boolean b=false;
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("phoneName",phoneName);
        map.put("phoneNumber",phoneNumber);
        map.put("empCode",userInfo.getUserCode());
        AddressBookEntity addressBookEntity=addressBookDbDao.selectUniqueByMap(map);
        if(addressBookEntity==null){
            b=true;
        }
        return b;
    }
}
